package com.xspace.android.xafcommon.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xspace.android.xafcommon.R;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.util.ResourceUtils;

import java.util.HashMap;

/**
 * Wubj 创建于 2017/1/9 0027.
 */
public class WebViewMoreDialog extends Dialog {

    private static final String TAG = WebViewMoreDialog.class.getSimpleName();

    private static HashMap<String, Integer> sKeyMap = new HashMap<>();

    static {
        String[] titles = ResourceUtils.getStringArray(R.array.more_groups);
        int[] pics = {R.drawable.icon_forward, R.drawable.icon_shoucang, R.drawable.icon_chakanjigou, R.drawable
                .icon_fenxiang, R.drawable.icon_ziti, R.drawable.icon_shuaxin, R.drawable.icon_fuzhi, R.drawable
                .icon_safari};
        for (int i = 0; i < titles.length; i++) {
            sKeyMap.put(titles[i], pics[i]);
        }
    }

//    private String[] titles = ResourceUtils.getStringArray(R.array.more_groups);
//    private String[] wnxTitls = {"收藏", "查看机构", "调整字号", "刷新"};
//    private int[] pics = {R.drawable.icon_shoucang, R.drawable.icon_chakanjigou, R.drawable.icon_ziti, R
//            .drawable.icon_shuaxin, R.drawable.icon_forward, R.drawable.icon_fuzhi, R.drawable
//            .icon_fenxiang, R.drawable.icon_safari};

    private Context mContext;
    private String[] usedTitle;
    private boolean isWnx = true;
    private GridView gridView;
    private RelativeLayout mLayout_cancel;

    public void setWnx(boolean isWnx) {
        this.isWnx = isWnx;
    }

    public interface IOperateClickListener {
        void clickListener(AdapterView<?> parent, View v, int position, long id);
    }

    private IOperateClickListener mListener;

    public void setmListener(IOperateClickListener mListener) {
        this.mListener = mListener;
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        getWindow().setGravity(Gravity.BOTTOM);
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = d.getWidth();
        getWindow().setAttributes(p);
    }

    private WebViewMoreDialog(Context context, String[] usedTitle) {
        this(context, R.style.CustomDialog);
        LogUtil.d(" context == null " + (context == null) + " usedTitle == null " + (usedTitle == null));
        this.mContext = context;
        this.usedTitle = usedTitle;
        View view = View.inflate(context, R.layout.dialog_webview_more, null);
        findView(view);
        super.setContentView(view);
    }

    private WebViewMoreDialog(Context context, int theme) {
        super(context, theme);
    }

    private void findView(View view) {
        try {
            gridView = (GridView) view.findViewById(R.id.gv_web_view_more);
            MoreAdapter adapter = new MoreAdapter();
            gridView.setAdapter(adapter);
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (mListener != null) {
                        mListener.clickListener(parent, view, position, id);
                    }
                    dismiss();
                }
            });
            mLayout_cancel = (RelativeLayout) view.findViewById(R.id.layout_web_view_more_cancel);
            mLayout_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class Builder {
        WebViewMoreDialog mDialog;

        public Builder(Context context, String[] usedTitle) {
            mDialog = new WebViewMoreDialog(context, usedTitle);
        }

        public WebViewMoreDialog build() {
            return mDialog;
        }
    }

    private class MoreAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return usedTitle.length;
        }

        @Override
        public String getItem(int position) {
            return usedTitle[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = viewHolder.holderView;
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.bindData(getItem(position));
            return convertView;
        }
    }

    class ViewHolder {
        View holderView;
        ImageView imageView;
        TextView textView;

        public ViewHolder() {
            holderView = initHolderView();
            holderView.setTag(this);
        }

        public View initHolderView() {
            View view = View.inflate(mContext, R.layout.item_dialog_webview_more, null);
            imageView = (ImageView) view.findViewById(R.id.iv_web_view_more);
            textView = (TextView) view.findViewById(R.id.tv_web_view_more);
            return view;
        }

        public void bindData(String title) {
            imageView.setImageResource(sKeyMap.get(title));
            textView.setText(title);
        }
    }


}
